﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using ARS.Control.Custom.Entity.Fields;

namespace ARS.Control.Custom.Entity.Forms
{
    public class ImageUploadForm:Form
    {
        public ImageUploadForm()
        {

        }

        protected override void RenderContents(HtmlTextWriter output)
        {

            string upload = string.Format("<script type='text/javascript'>function file_Select{0}(obj) {{var name = obj.value.split('//');$('#txtFileName{0}').val(name[name.length - 1]);}}</script><input type=\"text\" id=\"txtFileName{0}\" name=\"fileName\" style=\"width:180px\"/><input type=\"file\" id=\"file{0}\" name=\"{0}\" size=\"1\" onchange=\"file_Select{0}(this);\" style=\"position:absolute; width:10px; filter:alpha(opacity=0);-moz-opacity:0; top:0px;\" />  <input type=\"button\" class=\"btnCss\" value=\"上传\" onmousemove=\"document.getElementById('file{0}').style.top=(event.clientY-10)+'px'; document.getElementById('file{0}').style.left= (event.clientX)+'px';\" />", base.Field.Html_Control_Name);
            if (base.Field.Value == null)
            {
                output.Write(string.Format(base.GetTrHtml(), upload));
            }
            else
            {
                output.Write(string.Format(base.GetTrHtml(), string.Format("<img src=\"{0}\"  alt=\"{1}\" /><br/> {2}", base.Field.Value, "无", upload)));
            }

            base.RenderContents(output);
        }

        public override object GetInputValue()
        {

            HttpPostedFile file = HttpContext.Current.Request.Files[base.Field.Html_Control_Name];
            if (file != null && !string.IsNullOrEmpty(file.FileName))
            {
                ImageUploadField field = (ImageUploadField)base.Field;               
                if (!string.IsNullOrEmpty(field.SavePath))
                {
                    string pathDir = HttpContext.Current.Server.MapPath(field.SavePath);
                    if (!Directory.Exists(pathDir))
                    {
                        Directory.CreateDirectory(pathDir);
                    }
                }
                string xdpath = field.SavePath + DateTime.Now.ToString("yyMMddHHmmss") + file.FileName;
                //DateTime.Now.ToString("HH:mm:ss tt zz")
                string path = HttpContext.Current.Server.MapPath(xdpath);
                file.SaveAs(path);
                return xdpath;
            }
            else
            {
                //base.Field.Value = null;
//                if (base.Field.Value != null)
//                {
//                    base.Field.DefaultValue = base.Field.Value.ToString();
//                }
//                
//                return base.Field.Value;
                if (this.Page.IsPostBack)
                {
                    return null;
                }
                else
                {
                    return base.Field.DefaultValue;
                }
            }
            
        }

        
    }
}
